package com.punai.dataclean.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.punai.common.core.page.TableDataInfo;
import com.punai.common.exception.base.BaseException;
import com.punai.common.utils.PageUtils;
import com.punai.common.utils.ParseContentUtil;
import com.punai.dataclean.constants.CostDataConstanta;
import com.punai.dataclean.domain.CostDataClean;
import com.punai.dataclean.domain.CostDataCleanInfo;
import com.punai.dataclean.domain.vo.CostDataCleanVo;
import com.punai.dataclean.mapper.CostDataCleanInfoMapper;
import com.punai.dataclean.mapper.CostDataCleanMapper;
import com.punai.dataclean.service.CostDataCleanService;
import com.punai.externalccb.constants.CCBConstants;
import com.punai.externalccb.domain.EcoCbfeeitem;
import com.punai.externalccb.mapper.EcoCbfeeitemMapper;
import com.punai.standard.domain.*;
import com.punai.standard.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional
public class CostDataCleanServiceImpl implements CostDataCleanService {
    @Autowired
    private EcoCbfeeitemMapper cbfeeitemMapper;
    @Autowired
    private StaGxChapterMapper staGxChapterMapper;
    @Autowired
    private StaFbChapterMapper staFbChapterMapper;
    @Autowired
    private StaClChapterMapper staClChapterMapper;
    @Autowired
    private StaJxChapterMapper staJxChapterMapper;
    @Autowired
    private StaGxFeeMapper staGxFeeMapper;
    @Autowired
    private StaFbFeeMapper staFbFeeMapper;
    @Autowired
    private StaClFeeMapper staClFeeMapper;
    @Autowired
    private StaJxFeeMapper staJxFeeMapper;
    @Autowired
    private CostDataCleanMapper costDataCleanMapper;
    @Autowired
    private CostDataCleanInfoMapper costDataCleanInfoMapper;
    private static final String GX_ROOT_ID = "GX_ROOT_ID";
    private static final String CB_ROOT_ID = "CB_ROOT_ID";

    @Override
    public TableDataInfo getGroupTableData(CostDataCleanVo vo) {
        String versionId = vo.getVersionId();
        String chapterId = vo.getCategoryId();
        String gxCbType = vo.getGxCbType();
        String type = vo.getType();
        Map<String, String> codesMap = new HashMap<>();
        if (CostDataConstanta.Tabs.FB_0.getCode().equals(type)) {
            if (StrUtil.equals(GX_ROOT_ID, gxCbType)){
                List<String> chapterIds = new ArrayList<>();
                if (StrUtil.isNotEmpty(chapterId)) {
                    chapterIds.add(chapterId);
                    List<StaGxChapter> childByPid = staGxChapterMapper.selectList(Wrappers.<StaGxChapter>lambdaQuery()
                            .eq(StaGxChapter::getVersionId,versionId)
                            .apply(StrUtil.isNotEmpty(chapterId),"find_in_set('"+chapterId+"',pids)"));
                    if (CollUtil.isNotEmpty(childByPid)) {
                        chapterIds.addAll(childByPid.stream().map(StaGxChapter::getId).collect(Collectors.toList()));
                    }
                }
                List<StaGxFee> staGxFees = staGxFeeMapper.selectList(
                        Wrappers.<StaGxFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaGxFee::getChapterId, chapterIds)
                                .eq(StaGxFee::getVersionId, versionId)
                                .orderBy(true, true, StaGxFee::getCode)
                                .orderBy(true, true, StaGxFee::getOrderNum)
                );
                //获取所有费用项编码
                for (StaGxFee staGxFee : staGxFees) {
                    codesMap.put(staGxFee.getCode(), ParseContentUtil.parseContent(staGxFee.getContent()) + "@null" + staGxFee.getCalcRule());
                }
            } else if (StrUtil.equals(CB_ROOT_ID, gxCbType)){
                List<String> chapterIds = new ArrayList<>();
                if (StrUtil.isNotEmpty(chapterId)) {
                    chapterIds.add(chapterId);
                    List<StaFbChapter> childByPid = staFbChapterMapper.selectList(Wrappers.<StaFbChapter>lambdaQuery()
                            .eq(StaFbChapter::getVersionId, versionId)
                            .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                    if (CollUtil.isNotEmpty(childByPid)) {
                        chapterIds.addAll(childByPid.stream().map(StaFbChapter::getId).collect(Collectors.toList()));
                    }
                }
                List<StaFbFee> staFbFees = staFbFeeMapper.selectList(
                        Wrappers.<StaFbFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaFbFee::getChapterId, chapterIds)
                                .eq(StaFbFee::getVersionId, versionId)
                                .orderBy(true, true, StaFbFee::getCode)
                                .orderBy(true, true, StaFbFee::getOrderNum)
                );
                //获取所有费用项编码
                for (StaFbFee staFbFee : staFbFees) {
                    codesMap.put(staFbFee.getCode(), ParseContentUtil.parseContent(staFbFee.getContent()) + "@null" + staFbFee.getCalcRule());
                }
            }
        } else if (CostDataConstanta.Tabs.CL_1.getCode().equals(type)) {
            List<String> chapterIds = new ArrayList<>();
            if (StrUtil.isNotEmpty(chapterId)) {
                chapterIds.add(chapterId);
                List<StaClChapter> childByPid = staClChapterMapper.selectList(Wrappers.<StaClChapter>lambdaQuery()
                        .eq(StaClChapter::getVersionId, versionId)
                        .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                if (CollUtil.isNotEmpty(chapterIds)) {
                    chapterIds.addAll(childByPid.stream().map(StaClChapter::getId).collect(Collectors.toList()));
                }
            }
            List<StaClFee> staFbFees = staClFeeMapper.selectList(
                    Wrappers.<StaClFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaClFee::getChapterId, chapterIds)
                            .eq(StaClFee::getVersionId, versionId)
                            .orderBy(true, true, StaClFee::getCode)
                            .orderBy(true, true, StaClFee::getOrderNum)
            );
            //获取所有费用项编码
            for (StaClFee staclFee : staFbFees) {
                codesMap.put(staclFee.getCode(), "1" + "@null" + "1");
            }
        } else if (CostDataConstanta.Tabs.JX_2.getCode().equals(type)) {
            List<String> chapterIds = new ArrayList<>();
            if (StrUtil.isNotEmpty(chapterId)) {
                chapterIds.add(chapterId);
                List<StaJxChapter> childByPid = staJxChapterMapper.selectList(Wrappers.<StaJxChapter>lambdaQuery()
                        .eq(StaJxChapter::getVersionId, versionId)
                        .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                if (CollUtil.isNotEmpty(chapterIds)) {
                    chapterIds.addAll(childByPid.stream().map(StaJxChapter::getId).collect(Collectors.toList()));
                }
            }
            List<StaJxFee> staFbFees = staJxFeeMapper.selectList(
                    Wrappers.<StaJxFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaJxFee::getChapterId, chapterIds)
                            .eq(StaJxFee::getVersionId, versionId)
                            .orderBy(true, true, StaJxFee::getCode)
                            .orderBy(true, true, StaJxFee::getOrderNum)
            );
            //获取所有费用项编码
            for (StaJxFee stajxFee : staFbFees) {
                codesMap.put(stajxFee.getCode(), "1" + "@null" + "1");
            }
        } else if (CostDataConstanta.Tabs.JJ_3.getCode().equals(type)) {

        } else
            throw new BaseException("获取侧边树数据失败,分类有误");
        if (codesMap.keySet().size() == 0) return new TableDataInfo(Collections.emptyList(),0);
        Integer total = costDataCleanMapper.countAllGroupTableData(vo, codesMap.keySet(), getAllPriceType(vo.getPriceType()));
        PageUtils.startPage();
        List<CostDataCleanVo> costDataCleanVos = costDataCleanMapper.selectAllGroupTableData(vo, codesMap.keySet(), getAllPriceType(vo.getPriceType()));
        Map<String,String> finalCodesMap = codesMap;
        costDataCleanVos = costDataCleanVos.stream().peek(v -> {
            String staGxFees = finalCodesMap.get(v.getFeeCode());
            String[] split = staGxFees.split("@null");
            v.setFeeContent(split[0]);
            v.setFeeCalcrule(split[1]);
        }).collect(Collectors.toList());
        return new TableDataInfo(costDataCleanVos, total);
    }

    @Override
    public List<CostDataCleanVo> getDetailTableData(CostDataCleanVo vo) {
        return costDataCleanMapper.selectDetailTableData(vo, getAllPriceType(vo.getPriceType()))
                .stream()
                .peek(v-> v.setPriceType(CCBConstants.CCB_TYPE.getDescByCode(v.getPriceType())))
                .peek(v-> {
                    if(StrUtil.isNotBlank(v.getDataJson())){
                        EcoCbfeeitem ecoCbfeeitem = JSONUtil.toBean(v.getDataJson(), EcoCbfeeitem.class);
                        v.setOfeeCode(ecoCbfeeitem.getCode());
                        v.setOfeeName(ecoCbfeeitem.getName());
                    }
                })
                .collect(Collectors.toList());
    }

    @Override
    public Boolean enterClean(CostDataCleanVo vo) {
        Map<String, List<String>> selKey_ids = vo.getSelKeyLinkids();
        List<String> selKeys = vo.getSelKeys();
        String newKey = vo.getNewKey();
        String type = vo.getType();
        String cleanStatus = vo.getCleanStatus();
        if(StrUtil.isNotEmpty(newKey)){
            List<String> cleanIds = new ArrayList<>();
            for (String selKey : selKeys) {
                Map<String, String> formatKey = this.formatKey(selKey);
                List<String> ids = selKey_ids.get(selKey);
                if (StrUtil.isNotEmpty(formatKey.get("groupId"))) {
                    String groupId = formatKey.get("groupId");
                    costDataCleanInfoMapper.delete(Wrappers.<CostDataCleanInfo>lambdaQuery()
                            .eq(CostDataCleanInfo::getPid, groupId)
                            .in(CostDataCleanInfo::getOid, ids)
                    );
                    int size = costDataCleanInfoMapper.selectList(Wrappers.<CostDataCleanInfo>lambdaQuery().eq(CostDataCleanInfo::getPid, groupId)).size();
                    if (size == 0) {
                        costDataCleanMapper.deleteById(groupId);
                    } else {
                        CostDataClean costDataClean = costDataCleanMapper.selectById(groupId);
                        costDataClean.setYycs(size);
                        costDataCleanMapper.updateById(costDataClean);
                    }
                }
                cleanIds.addAll(ids);
            }
            Map<String, String> formatKey = this.formatKey(newKey);
            CostDataClean cleanData = new CostDataClean();
            cleanData.setCleanStatus(cleanStatus);
            cleanData.setCleanDate(StrUtil.equals("清洗数据", cleanData.getCleanStatus()) ? new Date() : null);
            cleanData.setClassification(type);
            cleanData.setFeeCode(formatKey.get("feeCode"));
            cleanData.setFeeName(formatKey.get("feeName"));
            cleanData.setFeeUnit(formatKey.get("feeUnit"));
            cleanData.setFeeSpec(formatKey.get("feeSpec"));
            cleanData.setYycs(cleanIds.size());
            cleanData = this.saveCostDataClean(cleanData);
            for (EcoCbfeeitem cbfeeitem : cbfeeitemMapper.selectList(Wrappers.<EcoCbfeeitem>lambdaQuery().in(EcoCbfeeitem::getId, cleanIds))) {
                CostDataCleanInfo info = new CostDataCleanInfo();
                info.setDataJson(JSONUtil.toJsonStr(cbfeeitem));
                info.setPid(cleanData.getId());
                info.setOid(cbfeeitem.getId());

                info.setFeeOcode(cleanData.getFeeCode());
                info.setFeeCode(cleanData.getFeeCode());
                info.setFeeName(cleanData.getFeeName());
                info.setFeeUnit(cleanData.getFeeUnit());
                info.setFeeSpec(cleanData.getFeeSpec());

                info.setFeePrice(cbfeeitem.getTargettaxprice());
                info.setPriceType(cbfeeitem.getStagetype());
                info.setProid(cbfeeitem.getProId());

                info.setFeeContent(cbfeeitem.getContent());
                info.setFeeCalcrule(cbfeeitem.getCalcrule());

                costDataCleanInfoMapper.insert(info);
            }
        }else{
            for (String selKey : selKeys) {
                Map<String, String> formatKey = this.formatKey(selKey);
                List<String> ids = selKey_ids.get(selKey);
                CostDataClean cleanData = new CostDataClean();
                if (StrUtil.isNotEmpty(formatKey.get("groupId"))) {
                    String groupId = formatKey.get("groupId");
                    costDataCleanInfoMapper.delete(Wrappers.<CostDataCleanInfo>lambdaQuery()
                            .eq(CostDataCleanInfo::getPid, groupId)
                            .in(CostDataCleanInfo::getOid, ids)
                    );
                    int size = costDataCleanInfoMapper.selectList(Wrappers.<CostDataCleanInfo>lambdaQuery().eq(CostDataCleanInfo::getPid, groupId)).size();
                    if (size == 0) {
                        costDataCleanMapper.deleteById(groupId);
                    }else{
                        CostDataClean costDataClean = costDataCleanMapper.selectById(groupId);
                        costDataClean.setYycs(size);
                        costDataCleanMapper.updateById(costDataClean);
                    }
                }
                cleanData.setCleanStatus(cleanStatus);
                cleanData.setCleanDate(StrUtil.equals("清洗数据", cleanData.getCleanStatus()) ? new Date() : null);
                cleanData.setClassification(type);
                cleanData.setFeeCode(formatKey.get("feeCode"));
                cleanData.setFeeName(formatKey.get("feeName"));
                cleanData.setFeeUnit(formatKey.get("feeUnit"));
                cleanData.setFeeSpec(formatKey.get("feeSpec"));
                cleanData.setYycs(ids.size());
                cleanData = this.saveCostDataClean(cleanData);
                for (EcoCbfeeitem cbfeeitem : cbfeeitemMapper.selectList(Wrappers.<EcoCbfeeitem>lambdaQuery().in(EcoCbfeeitem::getId, ids))) {
                    CostDataCleanInfo info = new CostDataCleanInfo();
                    info.setDataJson(JSONUtil.toJsonStr(cbfeeitem));
                    info.setPid(cleanData.getId());
                    info.setOid(cbfeeitem.getId());

                    info.setFeeOcode(cleanData.getFeeCode());
                    info.setFeeCode(cleanData.getFeeCode());
                    info.setFeeName(cleanData.getFeeName());
                    info.setFeeUnit(cleanData.getFeeUnit());
                    info.setFeeSpec(cleanData.getFeeSpec());

                    info.setFeePrice(cbfeeitem.getTargettaxprice());
                    info.setPriceType(cbfeeitem.getStagetype());
                    info.setProid(cbfeeitem.getProId());

                    info.setFeeContent(cbfeeitem.getContent());
                    info.setFeeCalcrule(cbfeeitem.getCalcrule());

                    costDataCleanInfoMapper.insert(info);
                }
            }
        }
        return true;
    }

    @Override
    public Boolean allEnterClean(CostDataCleanVo vo) {
        List<CostDataCleanVo> costDataCleanVos = new ArrayList<>();

        String versionId = vo.getVersionId();
        String chapterId = vo.getCategoryId();
        String gxCbType = vo.getGxCbType();
        String type = vo.getType();
        Map<String, String> codesMap = new HashMap<>();
        if (CostDataConstanta.Tabs.FB_0.getCode().equals(type)) {
            if (StrUtil.equals(GX_ROOT_ID, gxCbType)) {
                List<String> chapterIds = new ArrayList<>();
                if (StrUtil.isNotEmpty(chapterId)) {
                    chapterIds.add(chapterId);
                    List<StaGxChapter> childByPid = staGxChapterMapper.selectList(Wrappers.<StaGxChapter>lambdaQuery()
                            .eq(StaGxChapter::getVersionId, versionId)
                            .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                    if (CollUtil.isNotEmpty(childByPid)) {
                        chapterIds.addAll(childByPid.stream().map(StaGxChapter::getId).collect(Collectors.toList()));
                    }
                }
                List<StaGxFee> staGxFees = staGxFeeMapper.selectList(
                        Wrappers.<StaGxFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaGxFee::getChapterId, chapterIds)
                                .eq(StaGxFee::getVersionId, versionId)
                                .orderBy(true, true, StaGxFee::getCode)
                                .orderBy(true, true, StaGxFee::getOrderNum)
                );
                //获取所有费用项编码
                for (StaGxFee staGxFee : staGxFees) {
                    codesMap.put(staGxFee.getCode(), ParseContentUtil.parseContent(staGxFee.getContent()) + "@null" + staGxFee.getCalcRule());
                }
            } else if (StrUtil.equals(CB_ROOT_ID, gxCbType)) {
                List<String> chapterIds = new ArrayList<>();
                if (StrUtil.isNotEmpty(chapterId)) {
                    chapterIds.add(chapterId);
                    List<StaFbChapter> childByPid = staFbChapterMapper.selectList(Wrappers.<StaFbChapter>lambdaQuery()
                            .eq(StaFbChapter::getVersionId, versionId)
                            .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                    if (CollUtil.isNotEmpty(childByPid)) {
                        chapterIds.addAll(childByPid.stream().map(StaFbChapter::getId).collect(Collectors.toList()));
                    }
                }
                List<StaFbFee> staFbFees = staFbFeeMapper.selectList(
                        Wrappers.<StaFbFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaFbFee::getChapterId, chapterIds)
                                .eq(StaFbFee::getVersionId, versionId)
                                .orderBy(true, true, StaFbFee::getCode)
                                .orderBy(true, true, StaFbFee::getOrderNum)
                );
                //获取所有费用项编码
                for (StaFbFee staFbFee : staFbFees) {
                    codesMap.put(staFbFee.getCode(), ParseContentUtil.parseContent(staFbFee.getContent()) + "@null" + staFbFee.getCalcRule());
                }
            }
        } else if (CostDataConstanta.Tabs.CL_1.getCode().equals(type)) {
            List<String> chapterIds = new ArrayList<>();
            if (StrUtil.isNotEmpty(chapterId)) {
                chapterIds.add(chapterId);
                List<StaClChapter> childByPid = staClChapterMapper.selectList(Wrappers.<StaClChapter>lambdaQuery()
                        .eq(StaClChapter::getVersionId, versionId)
                        .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                if (CollUtil.isNotEmpty(chapterIds)) {
                    chapterIds.addAll(childByPid.stream().map(StaClChapter::getId).collect(Collectors.toList()));
                }
            }
            List<StaClFee> staFbFees = staClFeeMapper.selectList(
                    Wrappers.<StaClFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaClFee::getChapterId, chapterIds)
                            .eq(StaClFee::getVersionId, versionId)
                            .orderBy(true, true, StaClFee::getCode)
                            .orderBy(true, true, StaClFee::getOrderNum)
            );
            //获取所有费用项编码
            for (StaClFee staclFee : staFbFees) {
                codesMap.put(staclFee.getCode(), "1" + "@null" + "1");
            }
        } else if (CostDataConstanta.Tabs.JX_2.getCode().equals(type)) {
            List<String> chapterIds = new ArrayList<>();
            if (StrUtil.isNotEmpty(chapterId)) {
                chapterIds.add(chapterId);
                List<StaJxChapter> childByPid = staJxChapterMapper.selectList(Wrappers.<StaJxChapter>lambdaQuery()
                        .eq(StaJxChapter::getVersionId, versionId)
                        .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                if (CollUtil.isNotEmpty(chapterIds)) {
                    chapterIds.addAll(childByPid.stream().map(StaJxChapter::getId).collect(Collectors.toList()));
                }
            }
            List<StaJxFee> staFbFees = staJxFeeMapper.selectList(
                    Wrappers.<StaJxFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaJxFee::getChapterId, chapterIds)
                            .eq(StaJxFee::getVersionId, versionId)
                            .orderBy(true, true, StaJxFee::getCode)
                            .orderBy(true, true, StaJxFee::getOrderNum)
            );
            //获取所有费用项编码
            for (StaJxFee stajxFee : staFbFees) {
                codesMap.put(stajxFee.getCode(), "1" + "@null" + "1");
            }
        } else if (CostDataConstanta.Tabs.JJ_3.getCode().equals(type)) {

        } else
            throw new BaseException("获取侧边树数据失败,分类有误");
        if (codesMap.keySet().size() >0){
            costDataCleanVos = costDataCleanMapper.selectAllGroupTableData(vo, codesMap.keySet(), getAllPriceType(vo.getPriceType()));
        }


        vo.setNewKey(null);
        vo.setCleanStatus("清洗数据");
        Map<String, List<String>> selKey_ids = new HashMap<>();
        List<String> selKeys = new ArrayList<>();
        for (CostDataCleanVo costDataCleanVo : costDataCleanVos) {
            vo.setFeeCode(costDataCleanVo.getFeeCode());
            vo.setFeeSpec(costDataCleanVo.getFeeSpec());
            vo.setFeeName(costDataCleanVo.getFeeName());
            vo.setFeeUnit(costDataCleanVo.getFeeUnit());
            vo.setGroupId(costDataCleanVo.getId());
            List<CostDataCleanVo> detailTableData = this.getDetailTableData(vo);
            String key = (StrUtil.isBlank(costDataCleanVo.getId()) ? "@null" : costDataCleanVo.getId()) + "$$" +
                    (StrUtil.isBlank(costDataCleanVo.getFeeCode()) ? "@null" : costDataCleanVo.getFeeCode()) + "$$" +
                    (StrUtil.isBlank(costDataCleanVo.getFeeName()) ? "@null" : costDataCleanVo.getFeeName()) + "$$" +
                    (StrUtil.isBlank(costDataCleanVo.getFeeUnit()) ? "@null" : costDataCleanVo.getFeeUnit()) + "$$" +
                    (StrUtil.isBlank(costDataCleanVo.getFeeSpec()) ? "@null" : costDataCleanVo.getFeeSpec());
            selKeys.add(key);
            selKey_ids.put(key, detailTableData.stream().map(CostDataCleanVo::getId).collect(Collectors.toList()));
        }
        vo.setSelKeyLinkids(selKey_ids);
        vo.setSelKeys(selKeys);
        this.enterClean(vo);
        return true;
    }

    @Override
    public TableDataInfo getFeeData(CostDataCleanVo vo) {
        String versionId = vo.getVersionId();
        String chapterId = vo.getCategoryId();
        String gxCbType = vo.getGxCbType();
        String type = vo.getType();
        if (CostDataConstanta.Tabs.FB_0.getCode().equals(type)) {
            if (StrUtil.equals(GX_ROOT_ID, gxCbType)) {
                List<String> chapterIds = new ArrayList<>();
                if (StrUtil.isNotEmpty(chapterId)) {
                    chapterIds.add(chapterId);
                    List<StaGxChapter> childByPid = staGxChapterMapper.selectList(Wrappers.<StaGxChapter>lambdaQuery()
                            .eq(StaGxChapter::getVersionId, versionId)
                            .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                    if (CollUtil.isNotEmpty(childByPid)) {
                        chapterIds.addAll(childByPid.stream().map(StaGxChapter::getId).collect(Collectors.toList()));
                    }
                }
                Long selectCount = staGxFeeMapper.selectCount(
                        Wrappers.<StaGxFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaGxFee::getChapterId, chapterIds)
                                .eq(StaGxFee::getVersionId, versionId)
                                .orderBy(true, true, StaGxFee::getCode)
                                .orderBy(true, true, StaGxFee::getOrderNum)
                );
                PageUtils.startPage();
                List<StaGxFee> selectList = staGxFeeMapper.selectList(
                        Wrappers.<StaGxFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaGxFee::getChapterId, chapterIds)
                                .eq(StaGxFee::getVersionId, versionId)
                                .orderBy(true, true, StaGxFee::getCode)
                                .orderBy(true, true, StaGxFee::getOrderNum)
                );
                List<CostDataCleanVo> collect = selectList.stream().map(v -> {
                    CostDataCleanVo re = new CostDataCleanVo();
                    re.setId(v.getId());
                    re.setFeeCode(v.getCode());
                    re.setFeeName(v.getName());
                    re.setFeeUnit(v.getUnit());
                    re.setFeeContent(ParseContentUtil.parseContent(v.getContent()));
                    re.setFeeCalcrule(v.getCalcRule());
                    return re;
                }).collect(Collectors.toList());
                return new TableDataInfo(collect, selectCount.intValue());
            } else if (StrUtil.equals(CB_ROOT_ID, gxCbType)) {
                List<String> chapterIds = new ArrayList<>();
                if (StrUtil.isNotEmpty(chapterId)) {
                    chapterIds.add(chapterId);
                    List<StaFbChapter> childByPid = staFbChapterMapper.selectList(Wrappers.<StaFbChapter>lambdaQuery()
                            .eq(StaFbChapter::getVersionId, versionId)
                            .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                    if (CollUtil.isNotEmpty(childByPid)) {
                        chapterIds.addAll(childByPid.stream().map(StaFbChapter::getId).collect(Collectors.toList()));
                    }
                }
                Long selectCount = staFbFeeMapper.selectCount(
                        Wrappers.<StaFbFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaFbFee::getChapterId, chapterIds)
                                .eq(StaFbFee::getVersionId, versionId)
                                .orderBy(true, true, StaFbFee::getCode)
                                .orderBy(true, true, StaFbFee::getOrderNum)
                );
                PageUtils.startPage();
                List<StaFbFee> selectList = staFbFeeMapper.selectList(
                        Wrappers.<StaFbFee>lambdaQuery()
                                .in(chapterIds.size() > 0, StaFbFee::getChapterId, chapterIds)
                                .eq(StaFbFee::getVersionId, versionId)
                                .orderBy(true, true, StaFbFee::getCode)
                                .orderBy(true, true, StaFbFee::getOrderNum)
                );
                List<CostDataCleanVo> collect = selectList.stream().map(v -> {
                    CostDataCleanVo re = new CostDataCleanVo();
                    re.setId(v.getId());
                    re.setFeeCode(v.getCode());
                    re.setFeeName(v.getName());
                    re.setFeeUnit(v.getUnit());
                    re.setFeeContent(ParseContentUtil.parseContent(v.getContent()));
                    re.setFeeCalcrule(v.getCalcRule());
                    return re;
                }).collect(Collectors.toList());
                return new TableDataInfo(collect, selectCount.intValue());
            }
        } else if (CostDataConstanta.Tabs.CL_1.getCode().equals(type)) {
            List<String> chapterIds = new ArrayList<>();
            if (StrUtil.isNotEmpty(chapterId)) {
                chapterIds.add(chapterId);
                List<StaClChapter> childByPid = staClChapterMapper.selectList(Wrappers.<StaClChapter>lambdaQuery()
                        .eq(StaClChapter::getVersionId, versionId)
                        .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                if (CollUtil.isNotEmpty(chapterIds)) {
                    chapterIds.addAll(childByPid.stream().map(StaClChapter::getId).collect(Collectors.toList()));
                }
            }
            Long selectCount = staClFeeMapper.selectCount(
                    Wrappers.<StaClFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaClFee::getChapterId, chapterIds)
                            .eq(StaClFee::getVersionId, versionId)
                            .orderBy(true, true, StaClFee::getCode)
                            .orderBy(true, true, StaClFee::getOrderNum)
            );
            PageUtils.startPage();
            List<StaClFee> selectList = staClFeeMapper.selectList(
                    Wrappers.<StaClFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaClFee::getChapterId, chapterIds)
                            .eq(StaClFee::getVersionId, versionId)
                            .orderBy(true, true, StaClFee::getCode)
                            .orderBy(true, true, StaClFee::getOrderNum)
            );
            List<CostDataCleanVo> collect = selectList.stream().map(v -> {
                CostDataCleanVo re = new CostDataCleanVo();
                re.setId(v.getId());
                re.setFeeCode(v.getCode());
                re.setFeeName(v.getItemName());
                re.setFeeUnit(v.getUnit());
                return re;
            }).collect(Collectors.toList());
            return new TableDataInfo(collect, selectCount.intValue());
        } else if (CostDataConstanta.Tabs.JX_2.getCode().equals(type)) {
            List<String> chapterIds = new ArrayList<>();
            if (StrUtil.isNotEmpty(chapterId)) {
                chapterIds.add(chapterId);
                List<StaJxChapter> childByPid = staJxChapterMapper.selectList(Wrappers.<StaJxChapter>lambdaQuery()
                        .eq(StaJxChapter::getVersionId, versionId)
                        .apply(StrUtil.isNotEmpty(chapterId), "find_in_set('" + chapterId + "',pids)"));
                if (CollUtil.isNotEmpty(chapterIds)) {
                    chapterIds.addAll(childByPid.stream().map(StaJxChapter::getId).collect(Collectors.toList()));
                }
            }
            Long selectCount = staJxFeeMapper.selectCount(
                    Wrappers.<StaJxFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaJxFee::getChapterId, chapterIds)
                            .eq(StaJxFee::getVersionId, versionId)
                            .orderBy(true, true, StaJxFee::getCode)
                            .orderBy(true, true, StaJxFee::getOrderNum)
            );
            PageUtils.startPage();
            List<StaJxFee> selectList = staJxFeeMapper.selectList(
                    Wrappers.<StaJxFee>lambdaQuery()
                            .in(chapterIds.size() > 0, StaJxFee::getChapterId, chapterIds)
                            .eq(StaJxFee::getVersionId, versionId)
                            .orderBy(true, true, StaJxFee::getCode)
                            .orderBy(true, true, StaJxFee::getOrderNum)
            );
            List<CostDataCleanVo> collect = selectList.stream().map(v -> {
                CostDataCleanVo re = new CostDataCleanVo();
                re.setId(v.getId());
                re.setFeeCode(v.getCode());
                re.setFeeName(v.getItemName());
                re.setFeeUnit(v.getUnit());
                return re;
            }).collect(Collectors.toList());
            return new TableDataInfo(collect, selectCount.intValue());
        } else if (CostDataConstanta.Tabs.JJ_3.getCode().equals(type)) {

        } else
            throw new BaseException("获取侧边树数据失败,分类有误");
        return new TableDataInfo(Collections.emptyList(), 0);
    }

    private CostDataClean saveCostDataClean(CostDataClean one) {
        if (StrUtil.isNotEmpty(one.getId()))
            costDataCleanMapper.updateById(one);
        else
            costDataCleanMapper.insert(one);
        return one;
    }

    private Set<String> getAllPriceType(String oldPriceType){
        Set<String> priceTypes = new HashSet<>();
        if (StrUtil.equals("全部", oldPriceType)) {
            CostDataCleanService.CostProjectTypeKV.forEach(v -> priceTypes.add(v.get("key")));
        } else {
            priceTypes.add(oldPriceType);
        }
        return priceTypes;
    }

    private Map<String, String> formatKey(String key) {
        String[] split = key.split("\\$\\$");
        String groupId = split[0];
        String feeCode = split[1];
        String feeName = split[2];
        String feeUnit = split[3];
        String feeSpec = split[4];
        if (StrUtil.equals("@null", groupId)) groupId = null;
        if (StrUtil.equals("@null", feeCode)) feeCode = null;
        if (StrUtil.equals("@null", feeName)) feeName = null;
        if (StrUtil.equals("@null", feeUnit)) feeUnit = null;
        if (StrUtil.equals("@null", feeSpec)) feeSpec = null;

        String finalGroupId = groupId;
        String finalfeeCode = feeCode;
        String finalfeeName = feeName;
        String finalfeeUnit = feeUnit;
        String finalfeeSpec = feeSpec;
        return new HashMap<String, String>() {{
            put("groupId", finalGroupId);
            put("feeCode", finalfeeCode);
            put("feeName", finalfeeName);
            put("feeUnit", finalfeeUnit);
            put("feeSpec", finalfeeSpec);
        }};
    }
}
